/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.test;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.ThreadLocalRandom;

@SpringBootTest
public class IsfdOperatorParameterConfigTest {
    // 需要的字段列表
    private static final String[] TARGET_FIELDS = {
        "component", "custom_json_field", "field_id", "field_type",
        "label", "operator_name", "options", "path", "sort_order",
        "text_list", "title", "type"
    };

    private final ObjectMapper mapper = new ObjectMapper();
    private final ThreadLocalRandom random = ThreadLocalRandom.current();

    @Test
    public void test() throws Exception {
        // 1. 读取原始JSON文件
        File inputFile = new File("D:\\要素库项目\\参数库\\operator_parameter_config.json");
        ArrayNode originalArray = (ArrayNode) mapper.readTree(inputFile);

        // 2. 准备输出目录
        String outputDir = "D:\\要素库项目\\参数库\\new";
        new File(outputDir).mkdirs(); // 确保目录存在

        // 3. 生成10个文件
        int totalFiles = 10;
        int totalRecords = 1_000_000;
        int recordsPerFile = totalRecords / totalFiles;

        for (int fileIndex = 0; fileIndex < totalFiles; fileIndex++) {
            String outputFilePath = outputDir + "\\new_operator_parameter_config_" + (fileIndex + 1) + ".json";
            try (JsonGenerator generator = new JsonFactory().createGenerator(new FileWriter(outputFilePath))) {
                generator.useDefaultPrettyPrinter();
                generator.setCodec(mapper);
                generator.writeStartArray();

                int recordsGenerated = 0;
                while (recordsGenerated < recordsPerFile) {
                    for (JsonNode originalNode : originalArray) {
                        for (int i = 0; i < 350; i++) {
                            if (recordsGenerated >= recordsPerFile) break;

                            ObjectNode newNode = mapper.createObjectNode();
                            for (String field : TARGET_FIELDS) {
                                if (field.equals("custom_json_field")) {
                                    ArrayNode floatArray = generateRandomFloatArray();
                                    newNode.put(field, floatArray.toString());
                                } else if (field.equals("options") || field.equals("text_list")) {
                                    processArrayField(originalNode, newNode, field);
                                } else {
                                    copyFieldValue(originalNode, newNode, field);
                                }
                            }
                            generator.writeObject(newNode);
                            recordsGenerated++;
                        }
                        if (recordsGenerated >= recordsPerFile) break;
                    }
                }

                generator.writeEndArray();
            }
            System.out.println("文件 " + (fileIndex + 1) + " 生成完成！共生成 " + recordsPerFile + " 条记录");
        }
    }


    @Test
    public void test2() throws Exception {
        // 1. 读取原始JSON文件
        File inputFile = new File("D:\\要素库项目\\参数库\\operator_parameter_config.json");
        ArrayNode originalArray = (ArrayNode) mapper.readTree(inputFile);

        // 2. 准备输出目录
        String outputDir = "D:\\要素库项目\\参数库\\new2";
        new File(outputDir).mkdirs(); // 确保目录存在

        // 3. 生成10个文件
        int totalFiles = 10;
        int totalRecords = 1_000_000;
        int recordsPerFile = totalRecords / totalFiles;

        for (int fileIndex = 0; fileIndex < totalFiles; fileIndex++) {
            String outputFilePath = outputDir + "\\new_operator_parameter_config_" + (fileIndex + 1) + ".json";
            try (JsonGenerator generator = new JsonFactory().createGenerator(new FileWriter(outputFilePath))) {
                generator.useDefaultPrettyPrinter();
                generator.setCodec(mapper);
                generator.writeStartArray();

                int recordsGenerated = 0;
                while (recordsGenerated < recordsPerFile) {
                    for (int i = 0; i < 350; i++) {
                        for (JsonNode originalNode : originalArray) {
                            if (recordsGenerated >= recordsPerFile) break;

                            ObjectNode newNode = mapper.createObjectNode();
                            for (String field : TARGET_FIELDS) {
                                if (field.equals("custom_json_field")) {
                                    ArrayNode floatArray = generateRandomFloatArray();
                                    newNode.put(field, floatArray.toString());
                                } else if (field.equals("options") || field.equals("text_list")) {
                                    processArrayField(originalNode, newNode, field);
                                } else {
                                    copyFieldValue(originalNode, newNode, field);
                                }
                            }
                            generator.writeObject(newNode);
                            recordsGenerated++;
                        }
                        if (recordsGenerated >= recordsPerFile) break;
                    }
                }

                generator.writeEndArray();
            }
            System.out.println("文件 " + (fileIndex + 1) + " 生成完成！共生成 " + recordsPerFile + " 条记录");
        }
    }

    // 生成随机浮点数组
    private ArrayNode generateRandomFloatArray() {
        int size = random.nextInt(2, 6); // 2-5个元素
        ArrayNode array = mapper.createArrayNode();
        for (int i = 0; i < size; i++) {
            array.add(random.nextDouble() * 100); // 0-100之间的随机浮点数
        }
        return array;
    }

    // 处理数组字段转换为JSON字符串
    private void processArrayField(JsonNode source, ObjectNode target, String field) {
        if (source.has(field)) {
            JsonNode fieldNode = source.get(field);
            if (fieldNode.isArray()) {
                target.put(field, fieldNode.toString());
            } else {
                target.put(field, "");
            }
        } else {
            target.put(field, "");
        }
    }

    // 复制字段值或设为空字符串
    private void copyFieldValue(JsonNode source, ObjectNode target, String field) {
        if (source.has(field)) {
            target.set(field, source.get(field));
        } else {
            target.put(field, "");
        }
    }
}
